<?php
error_reporting(E_ALL);

define('MYAPP_CONFIG_HOME','/home/ahundiak/ws2009/');


/* Wrap everything in a class to avoid global variables */
class WebIndex
{
	static function run()
	{
		// include paths
		ini_set('include_path','.' . 
			PATH_SEPARATOR . MYAPP_CONFIG_HOME . 'Cerad/library' .     
			PATH_SEPARATOR . MYAPP_CONFIG_HOME . 'osso2010/data/classes' .  
			PATH_SEPARATOR . MYAPP_CONFIG_HOME . 'osso2010/data/import'     
		);
		// session_start();
		date_default_timezone_set('US/Central');
		
        require_once 'Cerad/Loader.php';
        Cerad_Loader::registerAutoload();

        $webFlag = FALSE;
        $tmpFileName = 'data.xml';
        
        if (isset($_FILES['data-path']['tmp_name']))
        {
        	$tmpFileName = $_FILES['data-path']['tmp_name'];
        	$webFlag = TRUE;
          //$flag = move_uploaded_file($tmpFileName,'data.xml');
        }
        
        $import = new ImportReferees(NULL);
        
        $import->process($tmpFileName);
        
    	$results = array(
    		'success' => true,
    		'message' => $import->message,
    	
    		'file'    => $tmpFileName,
    		'count'   => sizeof($import->items),
    	    'items'   => $import->items,
//    		'files'   => $_FILES,
//    		'post'    => $_POST,
    	);
    	if ($webFlag) 
    	{
    		echo json_encode($results);
    		return;
    	}
    	Cerad_Debug::dump($results['items']);
    	
        // self::original();
	}
	function original()
	{
    	// sleep(1);
    
    	$fileName = $_FILES['data-path']['name'];
    
    	$results = array(
    		'success' => true,
    		'file'    => $fileName,
    		'count'   => sizeof($_POST),
    		'files'   => $_FILES,
    		'post'    => $_POST,
    	);
    	echo json_encode($results);
    	/* {
     	 * "success":true,
         * "file":"Area5cReferees20090819.xml",
         * "count":1,"files":
         * {"data-path":
         *     {"name":"Area5cReferees20090819.xml",
         *      "type":"text\/xml",
         *      "tmp_name":"C:\\xampp\\tmp\\php75.tmp",
         *      "error":0,
         *      "size":171608
         *  }},
         *  "post":{"name":"xxx"}}
         */
	}
}
WebIndex::run();
?>
